<?
/****************************************/
/************* IdeaLAN LLC **************/
/* Servers module where you can monitor */
/* add delete servers.                  */
/*                                      */
/****************************************/
/****************************************/
/* SCRIPT: servers.php                  */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
/****************************************/
/* SCRIPT: config.php                   */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
if (file_exists("servers.php")) {	$mod_lud = filemtime("servers.php"); }		
$mod_ver	=	"0.8b";
/****************************************/
/*

SERVERMON	+
			- ADD IP LOOKUP.
END SERVERMON
*/


/* CONFIGURATION FILE */



/* ----------- INSTALLATION ----------- */

if ($_GET["view"] == "install") {
	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Server Monitor' AND `mod_location` = 'servers.php' AND `mod_ver` = '$mod_ver'";  
	$execute_check_module = mysql_query($check_module); 
	
	if ($execute_check_module != "") {
		if (mysql_num_rows($execute_check_module) == 0) { 
			
			
			/* *** CREATE SERVERS TABLE *** */
			$table_servers = "CREATE TABLE `".$DBprefix."servers` (
			`server_id` SMALLINT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`server_name` VARCHAR(50) NOT NULL ,
			`server_auth` VARCHAR(100) NOT NULL ,
			`server_ip` VARCHAR(20) NOT NULL ,
			`server_custid` VARCHAR(50) NOT NULL ,
			`server_upcount` SMALLINT(5) NOT NULL ,
			`server_lastupdate` VARCHAR(20) NOT NULL ,
			`server_notify` SMALLINT(5) NOT NULL ,
			`server_addby` VARCHAR(50) NOT NULL)";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_servers);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>SERVERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
 
			/* ---------- ADD MODULE TO MODULES LIST---------- */			
			// LETS CHECK IF THERE ARE ANY MODULES INSTALLED IF NOT LETS MAKE THIS ONE #1
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` DESC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			if ($row_count != 0) {
			if ($module = mysql_fetch_array($execute_get_modules)){

				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
			
			}
			
			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('$row_count' + 1,'Server Monitor','servers.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			} else {

			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('1','Server Monitor','servers.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			}
			


				
			header("Location: ../../in.php?show=config&view=modules&m=20#$row_count");
			die();
				
		} else {
			echo "ADDING MODULE: <i><b>SERVERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already added.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END INSTALL





/* -------- END INSTALLATION --------- */

/* ----------- REMOVAL ----------- */

if ($_GET["view"] == "remove") {
	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Server Monitor' AND `mod_location` = 'servers.php'";  
	$exexute_check_module = mysql_query($check_module); 
	
	if ($exexute_check_module != "") {
		if (mysql_num_rows($exexute_check_module) > 0) { 
			
			
			/* *** DROP PROJECTS TABLE *** */
			$table_servers = "DROP TABLE `".$DBprefix."servers`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_servers);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>SERVERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
 
			/* ---------- REMOVE MODULE FROM MODULES ---------- */	
			$remove_module = "DELETE FROM `".$DBprefix."modules` WHERE `mod_name` = 'Server Monitor' AND `mod_location` = 'servers.php'";
			$remove_result = mysql_query($remove_module);
			
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` ASC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			while ($module = mysql_fetch_array($execute_get_modules)){
				$i = $i + 1;
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
			
			$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_id` = '$i'  WHERE `mod_location` = '$mod_location'";
			$execute_upodate_modules = mysql_query($update_modules);
			
			}
			
				
			header("Location: ../../in.php?show=config&view=modules&m=21");
			die();
				
		} else {
			echo "REMOVING MODULE: <i><b>SERVERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already gone.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back.</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END REMOVAL

/* ----------   BACK UP MODULE ----------- */
if ($_GET["view"] == "backup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");	

	$backup_file = "Servers_". date("d-m-Y_H-i-s")  . ".gz";
	$backup = "mysqldump --opt -h $server -u$username -p$password $database $DBprefix"."servers | gzip > $backup_file";
	system($backup);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=22");
	die();	
	
} // END BACKUP


/* -----------  RESTORE MODULE ------------- */

if ($_GET["view"] == "restore") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	$restore_file = $_GET["file"];
	$restore = "zcat $restore_file | mysql -u$username -p$password $database";
	system($restore);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=25");
	die();	
	
} // END RESTORE

/* --------- CHANGE ORDER ------------ */
if ($_GET["view"] == "order") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	if ($_GET["change"] == "down") {
	
		$new_order = $_GET["modid"] + 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Server Monitor' AND `mod_location` =  'servers.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=31");
		die();
	}

	if ($_GET["change"] == "up") {

		$new_order = $_GET["modid"] - 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Server Monitor' AND `mod_location` =  'servers.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=32");
		die();
	
	}

} // END CHANGE ORDER

/* --------- UPDATE VERSION ------------ */

if ($_GET["view"] == "verup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	

	$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_ver` = '$mod_ver',`mod_lud` = '$mod_lud' WHERE `mod_location` = 'servers.php'";
	$execute_upodate_modules = mysql_query($update_modules);
	
			
	header("Location: ../../in.php?show=config&view=modules&m=33");
	die();
			
} // END UPDATE VERSION


/* -------- END INSTALATION --------- */


if ($_GET["view"] == "modify") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$modid = $_POST["modid"] . $_GET["modid"];

	if ($_POST["do"] == "add") {
		// DO ADD SERVER
		
		$server_name = $_POST["server_name"];
		$server_custid = $_POST["cust_ID"];
		$server_addby = $_POST["server_addby"];
		
		$server_auth = base64_encode($timedate);
		
		//echo "$server_name : $server_custid : $server_addby";
		$add_server = "INSERT INTO `".$DBprefix."servers` (`server_name` ,`server_auth` ,`server_custid` ,`server_upcount` ,`server_addby`) VALUES ('$server_name', '$server_auth', '$server_custid', '0', '$server_addby')";  
		$execute_add_server = mysql_query($add_server);

		header("Location: ../../in.php?show=mod&modid=$modid&m=26");
		die();	


	} // END DO ADD SERVER

	if ($_GET["do"] == "del") {
		// DELETE SERVER
		$server_id = $_GET["sid"];
		
		// DELETE SERVER
		$delete_server = "DELETE FROM `".$DBprefix."servers` WHERE `server_id` = '$server_id'";  
		$execute_delete_server = mysql_query($delete_server);

		header("Location: ../../in.php?show=mod&modid=$modid&m=27");
		die();	

	} // END DELETE SERVER

} // END MODIFY


if ($_GET["view"] == "check") { // SERVER CHECK
		 // WE NEED THE CONFIG FILE
		 
	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	$server_auth = $_GET["auth"];
	
	$check_servers = "SELECT * FROM `".$DBprefix."servers` WHERE `server_auth` LIKE '$server_auth' LIMIT 1";
	$execute_check_servers = mysql_query($check_servers); 
	
	if ($server = mysql_fetch_array($execute_check_servers)){
			// LETS GET SOME INFO ABOUT THIS SERVER
		$server_id = $server["server_id"];
		$server_name = $server["server_name"];
		$server_ip = $server["server_ip"];
		$server_upcount = $server["server_upcount"];
		$server_lastupdate = $server["server_lastupadate"];

		//echo "Authorized as: $server_id $server_name from $server_ip - Using KEY: $server_auth Update: $server_upcount"; 
		
		$update_server = "UPDATE `".$DBprefix."servers` SET `server_ip` = '$info_ipaddr',`server_upcount` = '$server_upcount' + 1,`server_lastupdate` = '$timedate' WHERE `server_id` = '$server_id'";  
		$result = mysql_query($update_server);

		die();
		
		} else { 
header ("Location: ../../in.php?e=0"); die(); 
}
	
	
	
	
	
	}

if (empty($_SESSION["sesid"])) { 
//header ("Location: ../../in.php?e=0"); 

die();}
?>

		<div id="content">
					<div id="left">
							<div class="naglowek"><div class="pkt"></div>Server Monitor</div>
							<ul id="me">
									<li <? if ($view == "") { echo "id=\"press\""; } ?> ><a href="in.php?show=mod&modid=<? echo $modid; ?>">Server List</a></li>
									<li <? if ($view == "add") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=add&modid=<? echo $modid; ?>">Add Server</a></li>

							</ul>
					
					
					</div>					
					
					<div id="right">
<?
if (!$view) {

// ATTACHE THIS TO THE PAGE, YOU WANT THE NEXT LINK AND PREVIOUS TO BE DISPLAYED
$result = mysql_query("SELECT * FROM `".$DBprefix."servers` ORDER BY `server_id` DESC"); 
$rownumbers = mysql_num_rows($result);
$linktest ="in.php?show=mod&modid=$modid";


include "inc/pages.php";


// LETS GET SERVERS FROM DATABASE

if ($sq == "") {
	$get_servers = "SELECT * FROM `".$DBprefix."servers` ORDER BY `server_id` DESC LIMIT $begin,20";
} else {
	$get_servers = "SELECT * FROM `".$DBprefix."servers` WHERE `server_name` LIKE '%$sq%' OR `server_custid` LIKE '%$sq%' ORDER BY `server_id` DESC LIMIT $begin,20"; 
}

$execute_get_servers = mysql_query($get_servers); 
$count_servers = mysql_num_rows($execute_get_servers);

echo "
						<div class='naglowek'><div class='pkt'></div>SERVER LIST</div>
							<div id='rightcont'>
<form method='POST' action='in.php?show=mod&modid=$modid'>

<center><lable>SEARCH: </lable><input name=\"sq\" type=\"text\" class=\"input_form1\"> <input type=\"submit\" value=\"SEARCH\" class=\"button1\"></center><br />

</form>
								<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='50' class='tableLogsHeader'>ID</td>
			<td width='200' class='tableLogsHeader'>NAME</td>
  			<td width='150' class='tableLogsHeader'>AUTH KEY</td>
  			<td width='150' class='tableLogsHeader'>IP</td>
  			<td width='100' class='tableLogsHeader'>STATUS</td>
  			<td width='100' class='tableLogsHeader'>COUNT</td>
  			<td width='100' class='tableLogsHeader'>OPTIONS</td>
		</tr>
";

while ($server = mysql_fetch_array($execute_get_servers)){
$server_id = $server["server_id"];
$server_name = $server["server_name"];
$server_auth = $server["server_auth"];
$server_ip = $server["server_ip"];
$server_custid = $server["server_custid"];
$server_upcount = $server["server_upcount"];
$server_lastupdate = $server["server_lastupdate"];
$server_addby = $server["server_addby"];


$server_green = $server_lastupdate + 600;
$server_yellow = $server_lastupdate + 900;
$server_red = $server_lastupdate + 1200;

if ($timedate <= $server_green) {
$server_status = "<img src='../g/ico/flag_green.png'>";

} else if ($timedate >= $server_yellow AND $timedate <= $server_red) {
$server_status = "<img src='../g/ico/flag_yellow.png'>";

} else if ($timedate >= $server_red) {
$server_status = "<img src='../g/ico/flag_red.png'>";

}








echo "<script type=\"text/javascript\">
function show_alert$server_id()
{
alert(\"*/10 * * * * lynx -dump http://office.idealan.us/members/inc/modules/servers.php?view=check&auth=$server_auth >/dev/null 2>&1\");
}
</script>


       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>&nbsp;$server_id</td>
	  		<td class='tableLogs'>&nbsp;$server_name</td>
  			<td class='tableLogs'>&nbsp;$server_auth</td>
  			<td class='tableLogs'>&nbsp;$server_ip</td>
  			<td class='tableLogs'>&nbsp;$server_status </td>
  			<td class='tableLogs'>&nbsp;$server_upcount</td>
			<td class='tableLogs'>$extra_links <a href='#' onclick=\"show_alert$server_id()\"><img src='../g/ico/server_link.png'></a> <a href=\"javascript:confirmDelete('inc/modules/servers.php?view=modify&do=del&sid=$server_id&modid=$modid','DELETING: $server_name')\"><img src='../g/ico/server_delete.png'></a> <a target=\"_BLANK\" href=\"https://$server_ip:10000/\"><img src='../g/ico/server_connect.png'></a></td>
		</tr>
";
}


echo "
</table><br />
<center>$link_prev $pagejumpline $link_next</center>
							</div>
";


}
 // END SERVER LIST

if ($view == "add") {

echo "
							<div class='naglowek'><div class='pkt'></div>Add a Server</div>
							<div id='rightcont'>
<form method='POST' action='./inc/modules/servers.php?view=modify'>
<input type='hidden' name='do' value='add' />
<input type='hidden' name='server_addby' value='$info_user_id' />
<input type='hidden' name='modid' value='$mod_id' />
<table style='width: 100%' class='editform1'>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Customer Name:</td>
		<td class='table3'><input class='input_form' type=\"text\" size=\"23\" id=\"cust\" name=\"cust\" value=\"\" autocomplete='off' onkeyup=\"ajax_showOptions(this,'getCountriesByLetters',event)\"><input type=\"hidden\" id=\"cust_hidden\" name=\"cust_ID\"> </td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Server Name:</td>
		<td class='table3'><input name=\"server_name\" type=\"text\" class=\"input_form\" id=\"server_name\" value=\"\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>

</table>
<p>&nbsp;</p>

    <input type=\"submit\" class=\"button1\" value=\"SAVE\">
</form>

";

} // END CLOSED S/O


?>

</div>
